package com.ecreditpal.audio.platform.ali;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.concurrent.CountDownLatch;

import com.alibaba.idst.nls.realtime.NlsClient;
import com.alibaba.idst.nls.realtime.NlsFuture;
import com.alibaba.idst.nls.realtime.protocol.NlsRequest;
import com.ecreditpal.audio.AsrRunner;
import com.ecreditpal.audio.platform.Config;
import lombok.extern.slf4j.Slf4j;



@Slf4j
public class RealtimeAsr  {
    protected static final NlsClient client = new NlsClient();
    RealTimeListener realTimeListener = new RealTimeListener();
    private NlsRequest req;

    static {
        log.debug("init Nls client...");
        client.init();

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                    client.close();
            }
        });
    }

    public RealtimeAsr() {

    }


    protected static final String asrSC = "pcm";
//    public String filePath = "";


    public void shutDown() {
        log.debug("close NLS client manually!");
        client.close();
        log.debug("demo done");
    }



    public void process(InputStream ins) {
        try {
            NlsFuture future = client.createNlsFuture(this.req, realTimeListener);
            log.info("call NLS service");
            byte[] b = new byte[3200];
            int len = 0;
            while ((len = ins.read(b)) > 0) {
                future.sendVoice(b, 0, len);
                Thread.sleep(200);
            }
            log.info("send finish signal!");
            future.sendFinishSignal();

            log.info("main thread enter waiting .");
//            future.await(100000);
//            this.response = realTimeListener.getFinalResult();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws FileNotFoundException {
        RealtimeAsr lun = new RealtimeAsr();
        Config config = new Config();
        config.setAppId("LTAI5psxDFoXnuDc");
        config.setAppKey("nls-service-realtime-8k");
        config.setAppSecret("vv3O2GJKN3c4bszDlXIKhLlWqfwJaT");
        lun.init(config);

        lun.process(new FileInputStream("/Users/lifeng/Desktop/pp.wav"));
		lun.shutDown();
    }


    public void init(Config config) {
        String appKey = "nls-service-realtime-8k";
        String ak_id = "LTAI5psxDFoXnuDc";
        String ak_secret = "vv3O2GJKN3c4bszDlXIKhLlWqfwJaT";

        this.req = new NlsRequest();
        req.setAppkey(config.getAppKey());
        req.setFormat(asrSC);
        req.setResponseMode("streaming");
        req.setSampleRate(8000);
        // 用户根据[热词文档](~~49179~~) 设置自定义热词。
        // 通过设置VocabularyId调用热词。
 req.setVocabularyId("");

        // 设置关键词库ID 使用时请修改为自定义的词库ID
req.setKeyWordListId("c98f6d85aeb14c13b585f93d48457eb0");

        // the id and the id secret
        req.setVocabularyId("f7d0b9e9d7984389b0738d82eb58e71b");
        req.authorize(config.getAppId(),config.getAppSecret());
    }


}
